/// @tags: Construction
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
#define fail() return puts(typ == 1 ? "-1" : "0"), 0

using namespace std;

namespace BlueQuantum {

typedef long long LL;

constexpr int MAXN = 3e4 + 3, MAXK = 2e2 + 3, P = 998244353;

int n, k, typ;
int dis[MAXK][MAXN], rt[MAXK], up[MAXK][MAXN], fa[MAXN];
bool vis[MAXN];
vector<pair<int, int> > son[MAXN];

inline int main() {
  cin >> typ >> n >> k;
  for (int i = 1; i <= k; ++i) {
    for (int j = 1; j <= n; ++j) {
      cin >> dis[i][j];
      if (dis[i][j] == 0) rt[i] = j;
    }
  }
  for (int i = 1; i <= k; ++i) {
    for (int j = 1; j <= n; ++j)
      if (dis[1][j] + dis[i][j] == dis[1][rt[i]]) up[i][dis[1][j]] = j, vis[j] = true;
    for (int j = 1; j <= dis[1][rt[i]]; ++j) {
      if (up[i][j] == 0) fail();
      fa[up[i][j]] = up[i][j - 1];
    }
  }
  for (int i = 1; i <= n; ++i)
    if (vis[i] == false) {
      int mx = 0, pos = rt[1];
      for (int j = 1; j <= k; ++j) {
        int dep = (dis[1][rt[j]] + dis[1][i] - dis[j][i]) / 2;
        if (dep > mx) mx = dep, pos = up[j][dep];
      }
      son[pos].push_back(make_pair(dis[1][i], i));
    }
  if (typ == 1) {
    for (int i = 1; i <= n; ++i) {
      sort(son[i].begin(), son[i].end());
      for (int j = dis[1][i] + 1, k = 0, lst = i; k < son[i].size();
           ++j, lst = son[i][k - 1].second) {
        if (son[i][k].first != j) fail();
        while (k < son[i].size() && son[i][k].first == j) fa[son[i][k].second] = lst, k++;
      }
    }
    for (int i = 1; i <= n; ++i)
      if (fa[i]) cout << i << ' ' << fa[i] << '\n';
  } else {
    int res = 1;
    for (int i = 1; i <= n; ++i) {
      sort(son[i].begin(), son[i].end());
      for (int j = dis[1][i] + 1, k = 0, lst = 1, cnt; k < son[i].size(); ++j, lst = cnt) {
        if (son[i][k].first != j) fail();
        cnt = 0;
        while (k < son[i].size() && son[i][k].first == j) res = (LL)lst * res % P, cnt++, k++;
      }
    }
    cout << res;
  }
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("portal.in", "r", stdin);
  freopen("portal.out", "w", stdout);
#endif
#endif

  ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
